<?php

namespace App\Http\Controllers\Admin;

use App\Article;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    //
    public function index(){
        return view('admin/article/index')->withArticles(Article::all());
    }

    public function create(){
        return view('admin/article/create');
    }

    public function store(Request $request){
        $this->validate($request,[
	        'title' => 'required|unique:articles|max:255',
	        'body' => 'required',
        ]);
        $article = new Article();
        $article->title = $request->get('title');
        $article->body = $request->get('body');
        $article->user_id = $request->user()->id;
        if($article->save()){
        	return redirect('admin/article');
        }else{
        	return redirect()->back()->withInput()->withError('添加失败！');
        }
    }

    public function edit($id){
	    $article = new Article();
	    $data = $article->find($id);
    	return view('admin/article/edit')->withArticle($data);
    }

    public function update(Request $request,$id){
    	$this->validate($request,[
		    'title' => 'required|unique:articles,title,'.$id.'|max:255',
		    'body' => 'required',
	    ]);
	    $article = Article::find($id);
	    $article->title = $request->get('title');
	    $article->body = $request->get('body');
	    $article->user_id = $request->user()->id;
	    if($article->update()){
		    return redirect('admin/article');
	    }else{
		    return redirect()->back()->withInput()->withError('编辑失败！');
	    }
    }

    public function destroy($id){
    	Article::find($id)->delete();
    	return redirect()->back()->withInput()->withErrors('删除成功！');
    }
}
